<template>
  <div class="my-address">
    <van-nav-bar
      title="地址管理"
      left-text="返回"
      left-arrow
      :fixed="true"
      @click-left="back"
    />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="newAddress"
      @edit="editAddress"
    />
  </div>
</template>

<script>
import "../assets/less/address.less";
export default {
  data() {
    return {
      //选择地址的id
      chosenAddressId: "1",

      //地址列表数据
      list: [],
    };
  },

  created() {
    //
    this.getReceiveAddress();
    console.log(this.list)
  },

  methods: {
    //返回上一级
    back() {
      this.$router.go(-1);
    },

    //查询收货地址
    getReceiveAddress() {
      //获取token字符串
      let tokenString = localStorage.getItem("CSTK");
      if (!tokenString) {
        return;
      }

      this.$toast.loading({
        message: "加载中...",
        forbidClick: true,
        duration: 0,
        loadingType: "spinner",
      });

      this.axios({
        method: "GET",
        url: "/findAddress",
        params: {
          appkey: this.appkey,
          tokenString,
        },
      })
        .then((result) => {
          this.$toast.clear();
          console.log("地址 result ==>", result);
          if (result.data.code == 20000) {
              result.data.result.map(v => {
                let address = v.province + v.city + v.county + v.addressDetail;
                  this.list.push({
                    id: v.aid,
                      name: v.name,
                      tel: v.tel,
                      address,
                      isDefault: Boolean(v.isDefault)
                  })
                console.log('地址 result ==> ',  this.list);
              })
          }
        })
        .catch((err) => {
          this.$toast.clear();
          console.log("err ==>", err);
        });
    },

    //新增地址
    newAddress(){
        this.$router.push({name: 'NewAddress'})
    },


    //编辑地址
    editAddress(item){
        console.log('item ==> ', item);
        this.$router.push({name: 'NewAddress', query:{aid: item.id}});
    }
  },
};
</script>

